package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import gov.kqqzyyy.monkey.ydhl.R;
import objects.HE_XJJL_Detail;
import objects.HE_XJYS;

/**
 * Created by Monkey on 2017/4/12.
 * 宣教记录适配器
 */

public class XJJLListAdapter extends BaseExpandableListAdapter {

    private Map<HE_XJYS, List<HE_XJJL_Detail>> dataSets;
    private LayoutInflater inflater;
    private List<HE_XJYS> parentsList;

    public XJJLListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public void setDataSets(Map<HE_XJYS, List<HE_XJJL_Detail>> dataSets) {
        this.dataSets = dataSets;
        parentsList = new ArrayList<>();
        getParentList();
        Collections.sort(parentsList, (arg0, arg1) -> arg0.getYSBS().compareTo(arg1.getYSBS()));
        notifyDataSetChanged();
    }

    private void getParentList() {
        Set set = dataSets.keySet();
        for (Object aSet : set) {
            parentsList.add((HE_XJYS) aSet);
        }
    }

    @Override
    public int getGroupCount() {
        if (parentsList != null && parentsList.size() > 0) {
            return parentsList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null && dataSets.get(parentsList.get(groupPosition)) != null) {
            return dataSets.get(parentsList.get(groupPosition)).size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null) {
            return parentsList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (parentsList != null && parentsList.get(groupPosition) != null && dataSets.get(parentsList.get(groupPosition)) != null && dataSets.get(parentsList.get(groupPosition)).get(childPosition) != null) {
            return dataSets.get(parentsList.get(groupPosition)).get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_xjjl_parent, parent, false);
            holder.textView = convertView.findViewById(R.id.xjjl_xjys_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if (parentsList != null && parentsList.size() > 0) {
            HE_XJYS heXjys = parentsList.get(groupPosition);
            String ysmc = heXjys.getYSMC() == null ? "" : heXjys.getYSMC();
            holder.textView.setText(ysmc);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        List<HE_XJJL_Detail> list = dataSets.get(parentsList.get(groupPosition));
        Collections.sort(list, (arg0, arg1) -> arg0.getXMBS().compareTo(arg1.getXMBS()));
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_xjjl_child, parent, false);
            holder.xjjl_xmmc_text = convertView.findViewById(R.id.xjjl_xmmc_text);
            holder.xjjl_jsxj_cb = convertView.findViewById(R.id.xjjl_jsxj_cb);
            holder.xjjl_brxj_cb = convertView.findViewById(R.id.xjjl_brxj_cb);
            holder.xjjl_xjry_text = convertView.findViewById(R.id.xjjl_xjry_text);
            holder.xjjl_xjsj_text = convertView.findViewById(R.id.xjjl_xjsj_text);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            HE_XJJL_Detail he_xjjl_detail = list.get(childPosition);
            if (he_xjjl_detail != null) {
                String xmmc = he_xjjl_detail.getXMMC() == null ? "" : he_xjjl_detail.getXMMC();
                holder.xjjl_xmmc_text.setText(xmmc);
                boolean jsxj = he_xjjl_detail.getJSXJ() != null && he_xjjl_detail.getJSXJ().equals("1");
                holder.xjjl_jsxj_cb.setChecked(jsxj);
                boolean brxj = he_xjjl_detail.getBRXJ() != null && he_xjjl_detail.getBRXJ().equals("1");
                holder.xjjl_brxj_cb.setChecked(brxj);
                String xjry = he_xjjl_detail.getYHXM() == null ? "" : he_xjjl_detail.getYHXM();
                holder.xjjl_xjry_text.setText(xjry);
                String xjsj = he_xjjl_detail.getXJSJ() == null ? "" : he_xjjl_detail.getXJSJ().substring(0, 19);
                holder.xjjl_xjsj_text.setText(xjsj);
            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ParentViewHolder {
        private TextView textView;
    }

    private class ChildViewHolder {
        private TextView xjjl_xmmc_text;
        private CheckBox xjjl_jsxj_cb;
        private CheckBox xjjl_brxj_cb;
        private TextView xjjl_xjry_text;
        private TextView xjjl_xjsj_text;
    }
}
